import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.FileNotFoundException;

import javax.swing.JFrame;


public class Editor extends JFrame implements Runnable,MouseMotionListener,MouseListener,KeyListener  {
	
	public Editor(){
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension dim = toolkit.getScreenSize();
		addMouseMotionListener((MouseMotionListener)this);
		addMouseListener((MouseListener)this);
		addKeyListener((KeyListener)this);
		int screenX = dim.width/2;
		int screenY = dim.width/2;
		setSize(screenX,screenY);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
	}
	
	public void paint (Graphics g){
		Variable.dbImage = createImage(getWidth(), getHeight());
		Variable.dbg = Variable.dbImage.getGraphics();
		paintComponent(Variable.dbg);
		g.drawImage(Variable.dbImage, 0, 0, this);
	}
	public void paintComponent(Graphics g){
		DrawGraphics.paintScreen(g);
		g.drawRect(Variable.MouseX*32, Variable.MouseY*32, 32, 32);
		repaint();
	}
	
	public static void main(String[] args) {
		Editor a1 = new Editor();
		GetImages a2 = new GetImages();
		

	}
	
	
	@Override
	public void mouseClicked(MouseEvent e) {
		int x = (int)(e.getX()/32);
		int y = (int)(e.getY()/32);
		ChangeTile.change(x, y, Variable.CurrentTile);
	}
	
	
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		Variable.MouseX = (int)(e.getX()/32);
		Variable.MouseY = (int)(e.getY()/32);		
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		
		if(e.getKeyCode() == KeyEvent.VK_P ){
			System.out.println("Begginging Print of level array");
			SaveLevel.Save();
		}
		if(e.getKeyCode() == KeyEvent.VK_L ){
			System.out.println("Begginging Loading");
			try {
				LoadLevel.load();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		
		if(e.getKeyCode() == KeyEvent.VK_E ){
			if(Variable.CurrentTile < 10){
				Variable.CurrentTile += 1;
			}else{
				Variable.CurrentTile = 10;
			}
			System.out.println(Variable.CurrentTile);
		}
		if(e.getKeyCode() == KeyEvent.VK_Q && !(Variable.CurrentTile == 0)&& !(Variable.CurrentTile == 11) ){
			if(Variable.CurrentTile > 1){
				Variable.CurrentTile -= 1;
			}else{
				Variable.CurrentTile = 1;
			}
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
